/**********************************************************
    COPYRIGHT
        KND, Inc. (c) 2011
    CLASS NAME
        app.c
    DEPENDENCY

    HISTORY
		09/09/2011, created by Guangxing Liu
**********************************************************/
#include "app.h"
#include "platform_uart.h"
#include "platform_gpio.h"
#include "platform_config.h"
#include "platform_pwm.h"
#include "platform_adc.h"
#include "lpc177x_8x_adc.h"
#include "platform_timer.h"
#include "lpc177x_8x_i2c.h"
#include "platform_ssp.h"

volatile platform_uart_buffer app_uart0_send_buffer;
volatile platform_uart_buffer app_uart0_recv_buffer;
volatile platform_uart_buffer app_uart1_send_buffer;
volatile platform_uart_buffer app_uart1_recv_buffer;
volatile platform_uart_buffer app_uart2_send_buffer;
volatile platform_uart_buffer app_uart2_recv_buffer;
volatile platform_uart_buffer app_uart3_send_buffer;
volatile platform_uart_buffer app_uart3_recv_buffer;
volatile hyacinth_service *p_uart_service;
volatile hyacinth_service *p_gpio_service;
volatile hyacinth_service *p_pwm_service;
volatile hyacinth_service *p_adc_service;
volatile hyacinth_service *p_ssp_service;

extern volatile unsigned int DataTxFIFOCount;
extern volatile unsigned int DataRxFIFOCount;

volatile unsigned char current_channel;
volatile platform_timer_counter adc_time_to_convert;
volatile unsigned char adc_wait_to_convert;

volatile unsigned char g_key_xl_low_speed=0;
volatile unsigned char g_key_xl_high_speed=0;
volatile unsigned char g_key_xl_off=0;

volatile platform_timer_counter touch_time_to_convert;
volatile unsigned char g_app_touch_time_to_convert;

/************************************************************************
	FUNCTION NAME : app_uart_init
	INPUTS: 
		void *handle
	OUTPUTS:	
		none
	DESCRIPTION:	
		App Uart Init
************************************************************************/
void app_uart_init(void *handle)
{
	volatile platform_uart_config p_uart_config;

	p_uart_service = (hyacinth_service *)hyacinth_get_service_handle(handle,UART_DEVICE);


	if(p_uart_service)
	{
		p_uart_config.uart_port = PORT_UART0;

		p_uart_config.UARTConfigStruct.Baud_rate = BAUD_115200;
			
		p_uart_config.UARTConfigStruct.Databits = UART_DATABIT_8;

		p_uart_config.UARTConfigStruct.Parity = UART_PARITY_NONE;

		p_uart_config.UARTConfigStruct.Stopbits = UART_STOPBIT_1;
			
		p_uart_service->hanle =p_uart_service->device_interface.open();

		p_uart_service->device_interface.ioctl(p_uart_service->hanle,platform_uart_app_call_device_op_configuart);

		p_uart_service->device_interface.write(p_uart_service->hanle,(void*)&p_uart_config,sizeof(platform_uart_config));

		memset(&app_uart0_send_buffer,0,sizeof(platform_uart_buffer));
		
		memset(&app_uart0_recv_buffer,0,sizeof(platform_uart_buffer));

		app_uart0_send_buffer.finish=TRUE;
		
		app_uart0_send_buffer.uart_port = PORT_UART0;

		app_uart0_recv_buffer.uart_port = PORT_UART0;
		
		p_uart_service->device_interface.ioctl(p_uart_service->hanle,platform_uart_app_call_device_op_setsendbuffer);

		p_uart_service->device_interface.write(p_uart_service->hanle,(void*)&app_uart0_send_buffer,sizeof(platform_uart_buffer));

		p_uart_service->device_interface.ioctl(p_uart_service->hanle,platform_uart_app_call_device_op_setrecvbuffer);

		p_uart_service->device_interface.write(p_uart_service->hanle,(void*)&app_uart0_recv_buffer,sizeof(platform_uart_buffer));

		
		
	}
}


/************************************************************************
	FUNCTION NAME : app_uart_send_data
	INPUTS: 
		char * buffer,int length
	OUTPUTS:	
		none
	DESCRIPTION:	
		App Uart Send Data
************************************************************************/
void app_uart_send_data(unsigned char uart_port,unsigned char * buffer,unsigned int length)
{

	if((!buffer) ||length <=0) return;
	
	switch(uart_port)
	{
		case PORT_UART0:
			while(app_uart0_send_buffer.finish==FALSE);//last frame tx finish

			app_uart0_send_buffer.length = length;

			app_uart0_send_buffer.inPos = 0;

			memcpy(app_uart0_send_buffer.pBuffer,buffer,sizeof(unsigned char)*app_uart0_send_buffer.length);

			app_uart0_send_buffer.finish = FALSE;

			app_uart0_send_buffer.ready = TRUE;
			
			break;
		case PORT_UART1:
			while(app_uart1_send_buffer.finish==FALSE);//last frame tx finish

			app_uart1_send_buffer.length = length;

			app_uart1_send_buffer.inPos = 0;

			memcpy(app_uart1_send_buffer.pBuffer,buffer,sizeof(unsigned char)*app_uart1_send_buffer.length);

			app_uart1_send_buffer.finish = FALSE;

			app_uart1_send_buffer.ready = TRUE;
			break;
		case PORT_UART2:
			while(app_uart2_send_buffer.finish==FALSE);//last frame tx finish

			app_uart2_send_buffer.length = length;

			app_uart2_send_buffer.inPos = 0;

			memcpy(app_uart2_send_buffer.pBuffer,buffer,sizeof(unsigned char)*app_uart2_send_buffer.length);

			app_uart2_send_buffer.finish = FALSE;

			app_uart2_send_buffer.ready = TRUE;
			break;
		case PORT_UART3:
			while(app_uart3_send_buffer.finish==FALSE);//last frame tx finish

			app_uart3_send_buffer.length = length;

			app_uart3_send_buffer.inPos = 0;

			memcpy(app_uart3_send_buffer.pBuffer,buffer,sizeof(unsigned char)*app_uart3_send_buffer.length);

			app_uart3_send_buffer.finish = FALSE;

			app_uart3_send_buffer.ready = TRUE;
			break;
		default:
			break;
	}
}


/************************************************************************
	FUNCTION NAME : app_uart_recv_data
	INPUTS: 
		int task_id
	OUTPUTS:	
		none
	DESCRIPTION:	
		App uart recv data
************************************************************************/
void app_uart_recv_data(int task_id)
{
	if(app_uart1_recv_buffer.finish)
	{
		app_uart_send_data(PORT_UART1,app_uart1_recv_buffer.pBuffer,app_uart1_recv_buffer.length);

		memset(app_uart1_recv_buffer.pBuffer,0,sizeof(unsigned char)*BUFFER_SIZE);

		app_uart1_recv_buffer.length = 0;

		app_uart1_recv_buffer.inPos = 0;

		app_uart1_recv_buffer.finish = FALSE;
	}
}

/************************************************************************
	FUNCTION NAME : app_gpio_init
	INPUTS: 
		void *handle
	OUTPUTS:	
		none
	DESCRIPTION:	
		App Gpio Init
************************************************************************/
void app_gpio_init(void *handle)
{
	volatile platform_gpio_setdir p_gpio_config;
	volatile platform_gpio_setvalue p_gpio_value;
	
	p_gpio_service = (hyacinth_service *)hyacinth_get_service_handle(handle,GPIO_DEVICE);

	if(p_gpio_service)
	{

		//p.22
		p_gpio_config.dir = GPIO_DIR_INPUT;

		p_gpio_config.gpio_num = PORT_GPIO2;

		p_gpio_config.gpio.gpio_pin = 2;

		p_gpio_value.gpio_num = PORT_GPIO2;

		p_gpio_value.gpio.gpio_pin = 2;

		p_gpio_value.value = 0;
		
		p_gpio_service->hanle =p_gpio_service->device_interface.open();

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiodir);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_config,sizeof(platform_gpio_setdir));

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiovalue);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_value,sizeof(platform_gpio_setvalue));

		//p.23
		p_gpio_config.dir = GPIO_DIR_INPUT;

		p_gpio_config.gpio_num = PORT_GPIO2;

		p_gpio_config.gpio.gpio_pin = 3;

		p_gpio_value.gpio_num = PORT_GPIO2;

		p_gpio_value.gpio.gpio_pin = 3;

		p_gpio_value.value = 0;

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiodir);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_config,sizeof(platform_gpio_setdir));

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiovalue);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_value,sizeof(platform_gpio_setvalue));		

		//p.24
		p_gpio_config.dir = GPIO_DIR_INPUT;

		p_gpio_config.gpio_num = PORT_GPIO2;

		p_gpio_config.gpio.gpio_pin = 4;

		p_gpio_value.gpio_num = PORT_GPIO2;

		p_gpio_value.gpio.gpio_pin = 4;

		p_gpio_value.value = 0;

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiodir);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_config,sizeof(platform_gpio_setdir));

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiovalue);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_value,sizeof(platform_gpio_setvalue));	

		//p.25
		p_gpio_config.dir = GPIO_DIR_INPUT;

		p_gpio_config.gpio_num = PORT_GPIO2;

		p_gpio_config.gpio.gpio_pin = 5;

		p_gpio_value.gpio_num = PORT_GPIO2;

		p_gpio_value.gpio.gpio_pin = 5;

		p_gpio_value.value = 0;

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiodir);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_config,sizeof(platform_gpio_setdir));

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiovalue);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_value,sizeof(platform_gpio_setvalue));	

		//p.26
		p_gpio_config.dir = GPIO_DIR_INPUT;

		p_gpio_config.gpio_num = PORT_GPIO2;

		p_gpio_config.gpio.gpio_pin = 6;

		p_gpio_value.gpio_num = PORT_GPIO2;

		p_gpio_value.gpio.gpio_pin = 6;

		p_gpio_value.value = 0;

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiodir);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_config,sizeof(platform_gpio_setdir));

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiovalue);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_value,sizeof(platform_gpio_setvalue));	
		//p.27

		p_gpio_config.dir = GPIO_DIR_INPUT;

		p_gpio_config.gpio_num = PORT_GPIO2;

		p_gpio_config.gpio.gpio_pin = 7;

		p_gpio_value.gpio_num = PORT_GPIO2;

		p_gpio_value.gpio.gpio_pin = 7;

		p_gpio_value.value = 0;

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiodir);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_config,sizeof(platform_gpio_setdir));

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiovalue);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_value,sizeof(platform_gpio_setvalue));	

		//p.28
		p_gpio_config.dir = GPIO_DIR_INPUT;

		p_gpio_config.gpio_num = PORT_GPIO2;

		p_gpio_config.gpio.gpio_pin = 8;

		p_gpio_value.gpio_num = PORT_GPIO2;

		p_gpio_value.gpio.gpio_pin = 8;

		p_gpio_value.value = 0;

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiodir);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_config,sizeof(platform_gpio_setdir));

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiovalue);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_value,sizeof(platform_gpio_setvalue));	
	
		//p1.0
		p_gpio_config.dir = GPIO_DIR_INPUT;

		p_gpio_config.gpio_num = PORT_GPIO1;

		p_gpio_config.gpio.gpio_pin = 0;

		p_gpio_value.gpio_num = PORT_GPIO1;

		p_gpio_value.gpio.gpio_pin = 0;

		p_gpio_value.value = 0;

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiodir);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_config,sizeof(platform_gpio_setdir));

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiovalue);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_value,sizeof(platform_gpio_setvalue));	

		//p.11
		p_gpio_config.dir = GPIO_DIR_INPUT;

		p_gpio_config.gpio_num = PORT_GPIO1;

		p_gpio_config.gpio.gpio_pin = 1;

		p_gpio_value.gpio_num = PORT_GPIO1;

		p_gpio_value.gpio.gpio_pin = 1;

		p_gpio_value.value = 0;

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiodir);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_config,sizeof(platform_gpio_setdir));

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiovalue);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_value,sizeof(platform_gpio_setvalue));

		//p.14
		p_gpio_config.dir = GPIO_DIR_INPUT;

		p_gpio_config.gpio_num = PORT_GPIO1;

		p_gpio_config.gpio.gpio_pin = 4;

		p_gpio_value.gpio_num = PORT_GPIO1;

		p_gpio_value.gpio.gpio_pin = 4;

		p_gpio_value.value = 0;

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiodir);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_config,sizeof(platform_gpio_setdir));

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiovalue);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_value,sizeof(platform_gpio_setvalue));

		//p1.8
		p_gpio_config.dir = GPIO_DIR_INPUT;

		p_gpio_config.gpio_num = PORT_GPIO1;

		p_gpio_config.gpio.gpio_pin = 8;

		p_gpio_value.gpio_num = PORT_GPIO1;

		p_gpio_value.gpio.gpio_pin = 8;

		p_gpio_value.value = 0;

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiodir);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_config,sizeof(platform_gpio_setdir));

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiovalue);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_value,sizeof(platform_gpio_setvalue));

		
		//p0.6
		p_gpio_config.dir = GPIO_DIR_OUTPUT;

		p_gpio_config.gpio_num = PORT_GPIO0;

		p_gpio_config.gpio.gpio_pin = 6;

		p_gpio_value.gpio_num = PORT_GPIO0;

		p_gpio_value.gpio.gpio_pin = 6;

		p_gpio_value.value = 1;

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiodir);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_config,sizeof(platform_gpio_setdir));

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiovalue);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_value,sizeof(platform_gpio_setvalue));

		//p0.7
		p_gpio_config.dir = GPIO_DIR_OUTPUT;

		p_gpio_config.gpio_num = PORT_GPIO0;

		p_gpio_config.gpio.gpio_pin = 7;

		p_gpio_value.gpio_num = PORT_GPIO0;

		p_gpio_value.gpio.gpio_pin = 7;

		p_gpio_value.value = 1;

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiodir);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_config,sizeof(platform_gpio_setdir));

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiovalue);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_value,sizeof(platform_gpio_setvalue));


		//p0.8
		p_gpio_config.dir = GPIO_DIR_OUTPUT;

		p_gpio_config.gpio_num = PORT_GPIO0;

		p_gpio_config.gpio.gpio_pin = 8;

		p_gpio_value.gpio_num = PORT_GPIO0;

		p_gpio_value.gpio.gpio_pin = 8;

		p_gpio_value.value = 1;

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiodir);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_config,sizeof(platform_gpio_setdir));

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiovalue);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_value,sizeof(platform_gpio_setvalue));


		//p0.9
		p_gpio_config.dir = GPIO_DIR_OUTPUT;

		p_gpio_config.gpio_num = PORT_GPIO0;

		p_gpio_config.gpio.gpio_pin = 9;

		p_gpio_value.gpio_num = PORT_GPIO0;

		p_gpio_value.gpio.gpio_pin = 9;

		p_gpio_value.value = 1;

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiodir);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_config,sizeof(platform_gpio_setdir));

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiovalue);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_value,sizeof(platform_gpio_setvalue));


		//p0.15
		p_gpio_config.dir = GPIO_DIR_OUTPUT;

		p_gpio_config.gpio_num = PORT_GPIO0;

		p_gpio_config.gpio.gpio_pin = 15;

		p_gpio_value.gpio_num = PORT_GPIO0;

		p_gpio_value.gpio.gpio_pin = 15;

		p_gpio_value.value = 1;

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiodir);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_config,sizeof(platform_gpio_setdir));

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiovalue);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_value,sizeof(platform_gpio_setvalue));

		//p0.16
		p_gpio_config.dir = GPIO_DIR_OUTPUT;

		p_gpio_config.gpio_num = PORT_GPIO0;

		p_gpio_config.gpio.gpio_pin = 16;

		p_gpio_value.gpio_num = PORT_GPIO0;

		p_gpio_value.gpio.gpio_pin = 16;

		p_gpio_value.value = 1;

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiodir);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_config,sizeof(platform_gpio_setdir));

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiovalue);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_value,sizeof(platform_gpio_setvalue));


		//p0.17
		p_gpio_config.dir = GPIO_DIR_OUTPUT;

		p_gpio_config.gpio_num = PORT_GPIO0;

		p_gpio_config.gpio.gpio_pin = 17;

		p_gpio_value.gpio_num = PORT_GPIO0;

		p_gpio_value.gpio.gpio_pin = 17;

		p_gpio_value.value = 1;

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiodir);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_config,sizeof(platform_gpio_setdir));

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiovalue);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_value,sizeof(platform_gpio_setvalue));

		//p0.18
		p_gpio_config.dir = GPIO_DIR_OUTPUT;

		p_gpio_config.gpio_num = PORT_GPIO0;

		p_gpio_config.gpio.gpio_pin = 18;

		p_gpio_value.gpio_num = PORT_GPIO0;

		p_gpio_value.gpio.gpio_pin = 18;

		p_gpio_value.value = 1;

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiodir);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_config,sizeof(platform_gpio_setdir));

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_setiovalue);

		p_gpio_service->device_interface.write(p_gpio_service->hanle,(void*)&p_gpio_value,sizeof(platform_gpio_setvalue));
		
				
	}
}

/************************************************************************
	FUNCTION NAME : app_pwm_init
	INPUTS: 
		void *handle
	OUTPUTS:	
		none
	DESCRIPTION:	
		App Pwm Init
************************************************************************/
void app_pwm_init(void *handle)
{
	volatile platform_pwm_config p_pwm_config;
	
	p_pwm_service = (hyacinth_service *)hyacinth_get_service_handle(handle,PWM_DEVICE);

	if(p_pwm_service)
	{
		p_pwm_config.pwm_no = PORT_PWM1;
		p_pwm_config.pwm_channel= PWM_CHANNEL1;
		p_pwm_config.pwm_value =0;

		p_pwm_service->hanle =p_pwm_service->device_interface.open();

		p_pwm_service->device_interface.ioctl(p_pwm_service->hanle,platform_pwm_app_call_device_op_pwminit);

		p_pwm_service->device_interface.write(p_pwm_service->hanle,(void*)&p_pwm_config,sizeof(platform_pwm_config));

		p_pwm_config.pwm_no = PORT_PWM1;
		p_pwm_config.pwm_channel= PWM_CHANNEL2;
		p_pwm_config.pwm_value =0;

		p_pwm_service->device_interface.write(p_pwm_service->hanle,(void*)&p_pwm_config,sizeof(platform_pwm_config));


		p_pwm_config.pwm_no = PORT_PWM1;
		p_pwm_config.pwm_channel= PWM_CHANNEL4;
		p_pwm_config.pwm_value =0;

		p_pwm_service->device_interface.write(p_pwm_service->hanle,(void*)&p_pwm_config,sizeof(platform_pwm_config));

		p_pwm_config.pwm_no = PORT_PWM1;
		p_pwm_config.pwm_channel= PWM_CHANNEL5;
		p_pwm_config.pwm_value =0;

		p_pwm_service->device_interface.write(p_pwm_service->hanle,(void*)&p_pwm_config,sizeof(platform_pwm_config));

	}

	
}


/************************************************************************
	FUNCTION NAME : app_adc_init
	INPUTS: 
		void *handle
	OUTPUTS:	
		none
	DESCRIPTION:	
		App Adc Init
************************************************************************/
void app_adc_init(void *handle)
{
	volatile platform_adc_config p_adc_config;
 	
	p_adc_service = (hyacinth_service *)hyacinth_get_service_handle(handle,ADC_DEVICE);	

	if(p_adc_service)
	{
		p_adc_config.rate = 100000;//100kHZ

		p_adc_service->hanle =p_adc_service->device_interface.open();

		p_adc_service->device_interface.ioctl(p_adc_service->hanle,platform_adc_app_call_device_op_adcinit);

		p_adc_service->device_interface.write(p_adc_service->hanle,(void*)&p_adc_config,sizeof(platform_adc_config));

		adc_wait_to_convert = 0;

		current_channel =ADC_CHANNEL_2;

		adc_time_to_convert.counter =0;

		adc_time_to_convert.counter_timeout = 1;// 240us

		adc_time_to_convert.start =1;//convert now

	}
	
}



/************************************************************************
	FUNCTION NAME : app_gpio_get_value
	INPUTS: 
		int task_id
	OUTPUTS:	
		none
	DESCRIPTION:	
		app_gpio_get_value
************************************************************************/
void app_gpio_get_value(int task_id)
{
	volatile platform_gpio_setvalue p_gpio_value;
	volatile platform_pwm_config p_pwm_config;
	
	
	if(p_gpio_service)
	{
		//p2.2
		p_gpio_value.gpio_num = PORT_GPIO2;

		p_gpio_value.gpio.gpio_pin = 2;

		p_gpio_service->device_interface.ioctl(p_gpio_service->hanle,platform_gpio_app_call_device_op_getiovalue);

		p_gpio_service->device_interface.read(p_gpio_service->hanle,(void*)&p_gpio_value,sizeof(platform_gpio_setvalue));	

		if(!p_gpio_value.value)
		{
			g_key_xl_off =1;
			g_key_xl_low_speed =0;
			g_key_xl_high_speed=0;

			if(p_pwm_service)
			{
				p_pwm_config.pwm_no = PORT_PWM1;
				
 				p_pwm_config.pwm_channel= PWM_CHANNEL1;
				
				p_pwm_config.pwm_value =0;//0%	

				p_pwm_service->device_interface.ioctl(p_pwm_service->hanle,platform_pwm_app_call_device_op_setpwmvalue);

				p_pwm_service->device_interface.write(p_pwm_service->hanle,(void*)&p_pwm_config,sizeof(platform_pwm_config));
				
			}
		}

		//p2.3

		p_gpio_value.gpio.gpio_pin = 3;

		p_gpio_service->device_interface.read(p_gpio_service->hanle,(void*)&p_gpio_value,sizeof(platform_gpio_setvalue));	

		if(!p_gpio_value.value)
		{
			g_key_xl_low_speed =1;
			g_key_xl_off =0;
			g_key_xl_high_speed=0;
				
			if(p_pwm_service)
			{
				p_pwm_config.pwm_no = PORT_PWM1;
				
 				p_pwm_config.pwm_channel= PWM_CHANNEL1;
				
				p_pwm_config.pwm_value =76;//30%	

				p_pwm_service->device_interface.ioctl(p_pwm_service->hanle,platform_pwm_app_call_device_op_setpwmvalue);

				p_pwm_service->device_interface.write(p_pwm_service->hanle,(void*)&p_pwm_config,sizeof(platform_pwm_config));
				
			}

		}

		//p2.4
		p_gpio_value.gpio.gpio_pin = 4;

		p_gpio_service->device_interface.read(p_gpio_service->hanle,(void*)&p_gpio_value,sizeof(platform_gpio_setvalue));	


		if(!p_gpio_value.value)
		{
			g_key_xl_high_speed =1;
			g_key_xl_off =0;
			g_key_xl_low_speed =0;
			
			if(p_pwm_service)
			{
				p_pwm_config.pwm_no = PORT_PWM1;
				
 				p_pwm_config.pwm_channel= PWM_CHANNEL1;
				
				p_pwm_config.pwm_value =128;//50%	

				p_pwm_service->device_interface.ioctl(p_pwm_service->hanle,platform_pwm_app_call_device_op_setpwmvalue);

				p_pwm_service->device_interface.write(p_pwm_service->hanle,(void*)&p_pwm_config,sizeof(platform_pwm_config));
				
			}
		}		

		p_gpio_value.gpio.gpio_pin = 5;

		p_gpio_service->device_interface.read(p_gpio_service->hanle,(void*)&p_gpio_value,sizeof(platform_gpio_setvalue));	

		p_gpio_value.gpio.gpio_pin = 6;

		p_gpio_service->device_interface.read(p_gpio_service->hanle,(void*)&p_gpio_value,sizeof(platform_gpio_setvalue));	

		p_gpio_value.gpio.gpio_pin = 7;

		p_gpio_service->device_interface.read(p_gpio_service->hanle,(void*)&p_gpio_value,sizeof(platform_gpio_setvalue));	

		p_gpio_value.gpio.gpio_pin = 8;

		p_gpio_service->device_interface.read(p_gpio_service->hanle,(void*)&p_gpio_value,sizeof(platform_gpio_setvalue));

		p_gpio_value.gpio.gpio_pin = 9;

		p_gpio_service->device_interface.read(p_gpio_service->hanle,(void*)&p_gpio_value,sizeof(platform_gpio_setvalue));

		p_gpio_value.gpio_num = PORT_GPIO1;

		p_gpio_value.gpio.gpio_pin = 0;

		p_gpio_service->device_interface.read(p_gpio_service->hanle,(void*)&p_gpio_value,sizeof(platform_gpio_setvalue));	

		//p_gpio_value.gpio.gpio_pin = 1;

		//p_gpio_service->device_interface.read(p_gpio_service->hanle,(void*)&p_gpio_value,sizeof(platform_gpio_setvalue));	

		//p_gpio_value.gpio.gpio_pin = 4;

		//p_gpio_service->device_interface.read(p_gpio_service->hanle,(void*)&p_gpio_value,sizeof(platform_gpio_setvalue));	

		//p_gpio_value.gpio.gpio_pin = 8;

		//p_gpio_service->device_interface.read(p_gpio_service->hanle,(void*)&p_gpio_value,sizeof(platform_gpio_setvalue));	

	}
}

/************************************************************************
	FUNCTION NAME : app_adc_begin_to_convation
	INPUTS: 
		channel
	OUTPUTS:	
		none
	DESCRIPTION:	
		app_adc_begin_to_convation
************************************************************************/
void app_adc_begin_to_convation(unsigned char channel)
{
	volatile platform_adc_config p_adc_config;
	
	p_adc_config.channel = channel;

	p_adc_config.channel_intr = channel;

	p_adc_service->device_interface.ioctl(p_adc_service->hanle,platform_adc_app_call_device_op_adcconfig);

	p_adc_service->device_interface.write(p_adc_service->hanle,(void*)&p_adc_config,sizeof(platform_adc_config));	
}

	volatile float temp_adc=0;
/************************************************************************
	FUNCTION NAME : app_adc_time_to_get_value
	INPUTS: 
		channel
	OUTPUTS:	
		none
	DESCRIPTION:	
		app_adc_time_to_get_value
************************************************************************/
void app_adc_time_to_get_value(unsigned char channel)
{
	volatile platform_adc_value p_adc_value;

	volatile unsigned int  complete=-1;

	volatile platform_pwm_config p_pwm_config;
	
	p_adc_value.channel = channel;
		
	p_adc_service->device_interface.ioctl(p_adc_service->hanle,platform_adc_app_call_device_op_getadcvalue);

	complete =p_adc_service->device_interface.read(p_adc_service->hanle,(void*)&p_adc_value,sizeof(platform_adc_value));

	if(complete==0)//convation complete
	{
		adc_wait_to_convert =0;

		adc_time_to_convert.start =1;
		
		switch(channel)
		{
			case ADC_CHANNEL2:
				
				if(p_pwm_service)
				{	
					temp_adc =p_adc_value.value>>4;
					
					if(temp_adc>=6)
					{
						temp_adc =6;
					}
					p_pwm_config.pwm_no = PORT_PWM1;
							
			 		p_pwm_config.pwm_channel= PWM_CHANNEL5;

					temp_adc =1-((float)(temp_adc)/(float)6);
							
					p_pwm_config.pwm_value =(unsigned char) (temp_adc*0xcc);

					p_pwm_service->device_interface.ioctl(p_pwm_service->hanle,platform_pwm_app_call_device_op_setpwmvalue);

					p_pwm_service->device_interface.write(p_pwm_service->hanle,(void*)&p_pwm_config,sizeof(platform_pwm_config));
							
				}
				

				current_channel = ADC_CHANNEL2;
				
				
				 break;

			case ADC_CHANNEL6:
					
				if(p_pwm_service)
				{	
					
					p_pwm_config.pwm_no = PORT_PWM1;
							
			 		p_pwm_config.pwm_channel= PWM_CHANNEL5;
							
					p_pwm_config.pwm_value = (unsigned char)(p_adc_value.value>>4);

					p_pwm_service->device_interface.ioctl(p_pwm_service->hanle,platform_pwm_app_call_device_op_setpwmvalue);

					p_pwm_service->device_interface.write(p_pwm_service->hanle,(void*)&p_pwm_config,sizeof(platform_pwm_config));
							
				}

				current_channel = ADC_CHANNEL2;
				break;
			default:
				break;
		}
	}else
	{
		adc_wait_to_convert =1;
	}
}

/************************************************************************
	FUNCTION NAME : app_I2C_init
	INPUTS: 
		none
	OUTPUTS:	
		none
	DESCRIPTION:	
		app_I2C_init
************************************************************************/
void app_I2C_init(void *handle)
{
	PINSEL_ConfigPin (0, 27, 1);
	PINSEL_ConfigPin (0, 28, 1);

	I2C_Init(I2C_0, 100000);

	I2C_Cmd(I2C_0, ENABLE);

}

/************************************************************************
	FUNCTION NAME : app_I2C_read_data
	INPUTS: 
		int task_id
	OUTPUTS:	
		none
	DESCRIPTION:	
		app_I2C_read_data
************************************************************************/
void app_I2C_read_data(int task_id)
{
	volatile int delay=0;

	unsigned char version=0;
	
	Saa7113_Read(I2C_0,0x00,&version);

	app_uart_send_data(PORT_UART0,&version,1);

	for(delay=0;delay<100000;++delay);
}
/************************************************************************
	FUNCTION NAME : app_touch_init
	INPUTS: 
		none
	OUTPUTS:	
		none
	DESCRIPTION:	
		app touch init
************************************************************************/
void app_touch_init(void *handle)
{
	touch_time_to_convert.start =1;

	touch_time_to_convert.counter =0;

	touch_time_to_convert.counter_timeout = 10000/BASE_TIME_TICK/8;//10ms

	g_app_touch_time_to_convert =0;
}


/************************************************************************
	FUNCTION NAME : app_ssp_init
	INPUTS: 
		none
	OUTPUTS:	
		none
	DESCRIPTION:	
		app ssp init
************************************************************************/
void app_ssp_init(void *handle)
{
	volatile platform_ssp_config p_ssp_config;
	
	p_ssp_service = (hyacinth_service *)hyacinth_get_service_handle(handle,SSP_DEVICE);	

	if(p_ssp_service)
	{
		p_ssp_config.ssp_no = PORT_SSP1;

		p_ssp_service->hanle =p_ssp_service->device_interface.open();

		p_ssp_service->device_interface.ioctl(p_ssp_service->hanle,platform_ssp_app_call_device_op_sspinit);

		p_ssp_service->device_interface.write(p_ssp_service->hanle,(void*)&p_ssp_config,sizeof(platform_ssp_config));		
	}
}

